package com.think.oauth.user.security;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableResourceServer;
import org.springframework.security.oauth2.config.annotation.web.configuration.ResourceServerConfigurerAdapter;
import org.springframework.security.oauth2.config.annotation.web.configurers.ResourceServerSecurityConfigurer;
import org.springframework.security.oauth2.provider.token.TokenStore;
import org.springframework.web.cors.CorsUtils;

import com.think.oauth.user.exception.AuthExceptionEntryPoint;
import com.think.oauth.user.exception.CustomAccessDeniedHandler;
import com.think.oauth.user.security.handle.CustomAuthenticationFailedHandler;
import com.think.oauth.user.security.handle.CustomAuthenticationSuccessHandler;


@Configuration
@EnableResourceServer
public class CustomResourceServerConfig extends ResourceServerConfigurerAdapter {

	private CustomAuthenticationFailedHandler failedHandler;

	private CustomAuthenticationSuccessHandler successHandler;

	

	@Autowired
	public TokenStore jwtTokenStore;
	@Override
	public void configure(HttpSecurity http) throws Exception {
		http//跨域配置开始
        .cors().disable()
        .cors()
        .and()
        .authorizeRequests()
        .requestMatchers(CorsUtils::isPreFlightRequest)
        .permitAll()
        //跨域配置结束
        .and().authorizeRequests().antMatchers("/webjars/**","/fileUpload/**","/v2/api-docs", "/swagger-resources/configuration/ui",
                "/swagger-resources","/swagger-resources/configuration/security",
                "/swagger-ui.html","/course/coursebase/**","/v1/user/emailCode","/v1/user/register","/v1/user/retriveCode","/v1/user/retrive").permitAll().anyRequest().authenticated();
	
	
		
//		http
//        //跨域配置开始
//        .cors().disable()
//        .cors()
//        .and()
//        .authorizeRequests()
//        .requestMatchers(CorsUtils::isPreFlightRequest)
//        .permitAll()
//        //跨域配置结束
//        .and()
//        .requestMatchers().anyRequest()
//        .and()
//        .anonymous()
//        .and()
//        .authorizeRequests()
//        .antMatchers(
//                "/swagger/**",
//                "/v2/api-docs",
//                "/doc.html",
//                "/swagger-ui.html",
//                "/swagger-resources/**").permitAll()
//        .and()
//        .authorizeRequests()
//        .antMatchers("/**").authenticated();//配置所有访问控制，必须认证过后才可以访问
	
	}

	

	@Override
	public void configure(ResourceServerSecurityConfigurer resources) throws Exception {
		// TODO Auto-generated method stub
		resources.tokenStore(jwtTokenStore);
		resources.authenticationEntryPoint(new AuthExceptionEntryPoint())
        .accessDeniedHandler(new CustomAccessDeniedHandler());
		
	}
}
